package warmachineroster.grapheditor.command;

import org.eclipse.gef.commands.Command;

import warmachineroster.UnitRosterElement;
import warmachinesystem.Unit;

public class UnitMinMaxSizeCommand extends Command{
	
	private UnitRosterElement ure;

	public void setUnitRosterElement(UnitRosterElement ure) {
		this.ure = ure;
	}

	@Override
	public boolean canExecute() {
		return ure != null;
	}

	@Override
	public boolean canUndo() {
		return true;
	}

	@Override
	public void execute() {
		Unit unitType = (Unit) ure.getUnitType();
		int baseModelNumber = unitType.getBaseModelNumber();
		int modelCount = ure.getModelCount();
		int increment = unitType.getIncrement();
		int newModelCount = getNewModelCount(increment, baseModelNumber, modelCount);
		
		ure.setModelCount(newModelCount);
	}

	private int getNewModelCount(int increment, int baseModelNumber,
			int modelCount) {
		int newModelCount;
		if (baseModelNumber == modelCount){
			newModelCount = baseModelNumber + increment;
		}else {
			newModelCount = modelCount - increment;
		}
		return newModelCount;
	}

	@Override
	public void undo() {
		execute();
	}
	
	
}
